# -*- coding: utf-8 -*-
'''
@Date    : 2022/10/13 14:23
@Author  : Zhaozhengyun
@File    : myprocessor.py
@Desc    :调用imagededup（ahash，phash和dhash），ssim，md5和phashdup海量去重方法。
'''

from imagededup import *
from phashdup import *
from ssim_md5 import *
import time


def call(arg, *args, **kwargs):
    global img1_input, img2_input, input_type, load_folder, save_folder
    try:
        # 参数解析
        start_time = time.time()
        if "load_folder" in arg.keys():
            load_folder = arg["load_folder"]
        if "save_folder" in arg.keys():
            save_folder = arg["save_folder"]
        if "img1_input" in arg.keys():
            img1_input = arg["img1_input"]
        if "img2_input" in arg.keys():
            img2_input = arg["img2_input"]
        if "input_type" in arg.keys():
            input_type = arg["input_type"]
        # threshold = arg["threshold"]

        # 选择算法
        algo = "ssim"
        if "algo" in arg.keys():
            algo = arg["algo"]

        if algo == "ssim":
            # print(algo)
            # 检测默认参数是否被覆盖
            threshold = 0.85
            if "threshold" in arg.keys():
                threshold = arg["threshold"]
                # print(threshold)
            return ssim(img1_input, img2_input, threshold, input_type)

        if algo == "md5":
            # 检测默认参数是否被覆盖
            return md5(img1_input, img2_input, input_type)

        if algo == "ahash" or algo == "phash" or algo == "dhash":
            # print(algo)

            threshold = 0.85
            if "threshold" in arg.keys():
                threshold = arg["threshold"]
            result = imagededup(load_folder, save_folder, threshold, algo)
            end_time = time.time()
            sum_time = end_time - start_time
            print("运行时间：", sum_time)
            return result
        if algo == "phashdup":
            threshold = 2
            if "threshold" in arg.keys():
                threshold = arg["threshold"]
            result = phashdup(save_folder, load_folder, threshold)
            end_time = time.time()
            sum_time = end_time - start_time
            print("运行时间：", sum_time)
            return result
        # return {"msg": "检测失败", "code": -1}

    except:
        return {"msg": "检测失败", "code": -1}


if __name__ == '__main__':
    arg = {
        "load_folder": "./data/images",
        "save_folder": "./data/image_result",
        "img1_input": "http://img.ecej.com/dp/7e24b60d2a1cfd12e4e7db6b3e796812.jpg",
        "img2_input": "http://img.ecej.com/dp/13e4e4ea3f5b4623093d976b3ab6e449.jpg",
        "threshold": 2,
        "algo": "ssim",
        "input_type": "url"
    }
    print(call(arg))
